package com.zyk.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zyk.entity.Resource;
import com.zyk.entity.Role;
import com.zyk.entity.User;
import com.zyk.service.IResourceService;
import com.zyk.service.IRoleService;
import com.zyk.service.IUserService;
import com.zyk.vo.DataGridVo;
import com.zyk.vo.FinalResult;
import com.zyk.vo.ResourcetreeGridVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.*;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author feifeimao
 * @since 2017-11-20
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IRoleService roleService;

    /**
     * 跳转到用户管理页面
     */
    @RequestMapping("manager")
    public String manager() {
        return "admin/user/user";
    }

    /**
     * 跳转用户添加页面
     *
     * @return
     */
    @RequestMapping("addPage")
    public String addPage() {
        return "admin/user/userAdd";
    }

    /**
     * 跳转用户编辑页面
     *
     * @return
     */
    @RequestMapping("editPage")
    public String editPage(User user, Model model) {
        User u = userService.selectById(user.getId());
        List<Role> roles = roleService.selectRoleIdListByUserId(user.getId());
        Set<Long> roleIds = new HashSet<Long>();
        for (Role r:roles) {
            roleIds.add(r.getId());
        }
        model.addAttribute("roleIds",roleIds);
        model.addAttribute("user",u);
        return "admin/user/userEdit";
    }

    /**
     * 用户列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("dataGrid")
    public DataGridVo dataGrid() {
        return userService.dataGrid();
    }

    /**
     * 删除用户
     */
    @RequestMapping("delete")
    @ResponseBody
    public FinalResult delete(User user) {
        if (userService.deleteById(user.getId())) {
            return new FinalResult("删除成功", true);
        } else {
            return new FinalResult("删除失败", false);
        }
    }

    /**
     * 用户添加
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("add")
    @Transactional
    public FinalResult add(User user) {
        user.setCreateTime(new Date());
        if (userService.insert(user)) {
            return new FinalResult("添加成功", true);
        } else {
            return new FinalResult("添加失败", false);
        }
    }
    @RequestMapping("edit")
    @ResponseBody
    public FinalResult edit(User user){
        user.setCreateTime(userService.selectById(user.getId()).getCreateTime());
        if(userService.updateAllColumnById(user)){
            return new FinalResult("编辑成功",true);
        }else {
            return new FinalResult("编辑失败",false);
        }
    }
}
